package me.zixing.memorandum.ui.component;

import me.zixing.memorandum.domain.DayTask;
import me.zixing.memorandum.utils.AppConstant;
import me.zixing.memorandum.utils.JdbcUtils;
import me.zixing.memorandum.utils.TaskPanelUtils;
import me.zixing.memorandum.utils.TimingUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TaskPanel extends JPanel{
    private DayTask dayTask;
    private JLabel editLabel;
    private JLabel deleteLabel;
    public TaskPanel(DayTask dayTask) {
        this.dayTask = dayTask;
        this.setLayout(null);
        initUI();
        initAction();
    }


    public void initUI(){
        JLabel contentLabel = new JLabel();
        contentLabel.setText(dayTask.getTitle());
        contentLabel.setFont(AppConstant.SOFT_BOLD_15);
        contentLabel.setBounds(20,12,150,30);
        JLabel timeLabel = new JLabel();
        timeLabel.setText(dayTask.getStartDateTime()+"~"+dayTask.getEndDateTime());
        timeLabel.setBounds(20,40,100,30);
        timeLabel.setFont(AppConstant.SOFT_ITALIC_15);

        if(dayTask.getState() == 0){
            editLabel = new JLabel();
            editLabel.setBounds(175,30,32,32);
            editLabel.setIcon(new ImageIcon(TaskPanel.class.getClassLoader().getResource("edit.png")));

            deleteLabel = new JLabel();
            deleteLabel.setBounds(220,30,32,32);
            deleteLabel.setIcon(new ImageIcon(TaskPanel.class.getClassLoader().getResource("delete.png")));

            this.add(editLabel);
            this.add(deleteLabel);
        }

        this.add(timeLabel);
        this.add(contentLabel);
    }

    public void initAction(){
        if(deleteLabel != null){
            deleteLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int n = JOptionPane.showConfirmDialog(null, "你确定删除这个备忘?", "警告",JOptionPane.YES_NO_OPTION);
                    if(n == 0){
                        JdbcUtils.deleteDayTask(dayTask);
                        for (DayTask dayTaskInDayTask:TimingUtils.dayTasks) {
                            if(dayTaskInDayTask.getId() == dayTask.getId()){
                                dayTaskInDayTask = null;
                            }
                        }
                        JViewport jViewport = (JViewport)TaskPanel.this.getParent().getParent();
                        TaskPanelUtils.refreshTasksPanel(jViewport,dayTask.getDayDO());
                    }
                }
            });
        }
        if(editLabel != null){
            editLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    JViewport jViewport = (JViewport)TaskPanel.this.getParent().getParent();
                    FormPanel panel = new FormPanel(dayTask,jViewport);
                }
            });
        }

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        try{
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
